<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>内置的Symbol值</title>
</head>
<body>
    <h1>内置的Symbol值</h1>

    <script>
        // Symbol.hasInstance
        class MyArray {
            static [Symbol.hasInstance](instance) {
                return Array.isArray(instance);
            }
        }
        console.log([] instanceof MyArray); // true

        // Symbol.iterator
        let myIterable = {
            *[Symbol.iterator]() {
                yield 1;
                yield 2;
                yield 3;
            }
        };
        console.log([...myIterable]); // [1, 2, 3]

        // Symbol.toPrimitive
        let obj = {
            [Symbol.toPrimitive](hint) {
                switch (hint) {
                    case 'number':
                        return 123;
                    case 'string':
                        return 'str';
                    case 'default':
                        return 'default';
                }
            }
        };
        console.log(2 * obj); // 246
        console.log(String(obj)); // "str"
        console.log(obj + ''); // "default"
    </script>
</body>
</html> 